package d7_homework.d2_homework.d3_thirdquestion;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Test {
    //1、创建扫描器
    private static Scanner sc = new Scanner(System.in);
    //2、创建一个Map集合用于存储商品对象
    private static Map<Product, Integer> shoppingCart = new HashMap<>();

    public static void main(String[] args) {
        while (true) {
            //目标: HashMap, HashMap的遍历
            //提示用户要加入假如购物车的商品商城,与商品名称
            System.out.println("请您输入要商品的商城");
            String store = sc.next();


            //判断集合是否已经存在这个商品
            //卫语句判断
            if (store.equals("end")) {
                break;
            } else {
                System.out.println("请您输入商品的名称");
                String productName = sc.next();
                //封装到对象中去
                Product product = new Product(store, productName);
                if (shoppingCart.containsKey(product)) {
                    //添加的商品以及存在 数量加1
                    shoppingCart.put(product, shoppingCart.get(product) + 1);
                } else {
                    shoppingCart.put(product, 1);
                }
        /*//遍历集合判断是否是否已经存在一模一样的商品
        Set<Product> products = shoppingCart.keySet();*/
            }
        }
        //展示购物车信息
        System.out.println("购物车的信息如下:");
        shoppingCart.forEach((k, v) -> System.out.println("商品:" + k + "\t" + "数量:" + v));


    }
}
